package com.zdj.presentation.base;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import com.zdj.data.events.DefaultEvent;
import com.zdj.presentation.R;
import com.zdj.presentation.helpers.ConstantsHelper;
import com.zdj.presentation.helpers.UIHelper;
import com.zdj.utils.StringUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;


/**
 * Created by zdj on 16-5-28.
 */

public abstract class BaseActivity<VM extends BaseViewModel, VB extends ViewDataBinding> extends AppCompatActivity implements IBaseView {

    protected final String TAG = this.getClass().getSimpleName();

    protected VM viewModel;
    protected VB viewDatabinding;

    protected Toolbar mToolbar;
    protected ActionBar mActionBar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        EventBus.getDefault().register(this);

        /**
         * 设置为竖屏，在theme中统一设置无效，不明白
         */
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        viewDatabinding = DataBindingUtil.<VB>setContentView(this, getContentLayoutId());
        initVms();
        setViewModel2Binding();
        initView();
        initToolbar();

    }

    private void initToolbar() {
        mToolbar = (Toolbar)findViewById(R.id.toolbar);
        /**
         * 统一 设置 fitsSystemWindows属性
         * 暂时不可用
         */
        if (mToolbar != null) {
            // 设置toolbar 须在setSupportActionBar之前
            setSupportActionBar(mToolbar);
            mToolbar.setNavigationOnClickListener(view -> onBackPressed());
        }
        mActionBar = getSupportActionBar();
        if (hasActionBar()) {
            initActionBar(mActionBar);
        }


    }

    protected abstract void initVms();

    protected abstract int getContentLayoutId();

    protected abstract void setViewModel2Binding();

    protected abstract void initView();


    /**
     * 是否有返回按钮，默认有 <br/>
     * created at 2015/12/28 9:30
     */
    protected boolean hasBackButton() {
        return true;
    }

    /**
     * 默认有actionbar <br/>
     * 作者 ：dengjie zhang <br/>
     * created at 2016/1/25 15:09
     */
    protected boolean hasActionBar() {
        return true;
    }

    /**
     * 重写，隐藏title <br/>
     * modified by Administrator at 2016/1/29 17:10
     */
    protected void initActionBar(ActionBar actionBar) {
        if (actionBar == null)
            return;

        if (hasBackButton()) {
            actionBar.setDisplayUseLogoEnabled(false);
            actionBar.setDisplayShowTitleEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeButtonEnabled(true);
        } else {
            actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE);
            actionBar.setDisplayUseLogoEnabled(false);
            int titleRes = getActionBarTitle();
            if (titleRes != 0) {
                actionBar.setTitle(titleRes);
            }
        }
    }

    public void setActionBarTitle(int resId) {
        if (resId != 0) {
            setActionBarTitle(getString(resId));
        }
    }

    public void setActionBarTitle(String title) {
        if (StringUtils.isEmpty(title)) {
            title = getString(R.string.app_name);
        }
        if (hasActionBar() && mActionBar != null) {
            mActionBar.setTitle(title);
        }
    }

    protected int getActionBarTitle() {
        return R.string.app_name;
    }

    @Override
    public void showProgress(boolean flag, String message) {

    }

    @Override
    public void showProgress(String message) {

    }

    @Override
    public void showProgress() {

    }

    @Override
    public void showProgress(boolean flag) {

    }

    @Override
    public void hideProgress() {

    }

    @Override
    public void showToast(int resId) {

    }

    @Override
    public void showToast(String msg) {
        if (!isFinishing()) {
            UIHelper.showToast(this, msg);
        }
    }

    @Override
    public void close() {
        finish();
    }

    public void toActivity(Class clz) {
        Intent intent = new Intent(this, clz);
        startActivity(intent);
    }

    public void toActivity(Class clz, Bundle bundle) {
        Intent intent = new Intent(this, clz);
        intent.putExtra(ConstantsHelper.BUNDLE, bundle);
        startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }


    @Subscribe
    public void doNothing(DefaultEvent event) {
        // do nothing
    }
}
